package p200;

public class Leetcode_200 {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }
        int col = grid.length;
        int row = grid[0].length;
        int r = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1') {
                    r++;
                    dfs(grid, i, j, col, row);
                }
            }
        }
        return r;
    }

    private void dfs(char[][] grid, int r, int c, int col, int row) {
        int[] dx = {-1, 0, 1, 0};
        int[] dy = {0, -1, 0, 1};
        if (r < 0 || c < 0 || r >= row || c >= col || grid[r][c] == '0') {
            return;
        }
        grid[r][c] = '0';
        for (int k = 0; k < 4; k++) {
            dfs(grid, r + dx[k], c + dy[k], col, row);
        }
    }

    public static void main(String[] args) {
        Leetcode_200 l = new Leetcode_200();
        System.out.println(l.numIslands(new char[][]{}));
    }
}
